/**
 *Project: jh-framework
 *File: cn.xkshow.util.IpTool.java 
 *Author: admin@xkshow.cn with the copyrite
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-1-27        xkshow.cn         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.util;

import java.io.PrintStream;

public class IpTool
{
	/*
	 * IP of the string,long swaps 
	 */
  public static long ip2Long(String strIp)
  {
    if ((strIp == null) || (strIp.equals("")))
    {
      return -5877515998428397568L;
    }

    long[] ip = new long[4];

    int position1 = strIp.indexOf(".");
    int position2 = strIp.indexOf(".", position1 + 1);
    int position3 = strIp.indexOf(".", position2 + 1);

    ip[0] = Long.parseLong(strIp.substring(0, position1));
    ip[1] = Long.parseLong(strIp.substring(position1 + 1, position2));
    ip[2] = Long.parseLong(strIp.substring(position2 + 1, position3));
    ip[3] = Long.parseLong(strIp.substring(position3 + 1));

    return ((ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]);
  }
  
  public static String long2Ip(long longIp)
  {
    String temp = Long.toHexString(longIp);

    String ip = String.valueOf(Integer.decode("0x" + temp.substring(0, 2)));
    ip = ip + "." + String.valueOf(Integer.decode(new StringBuffer("0x").append(temp.substring(2, 4)).toString()));
    ip = ip + "." + String.valueOf(Integer.decode(new StringBuffer("0x").append(temp.substring(4, 6)).toString()));
    ip = ip + "." + String.valueOf(Integer.decode(new StringBuffer("0x").append(temp.substring(6, 8)).toString()));

    return ip;
  }
  
}
